<?php


namespace app\api\controller;

use think\Db;
use think\Exception;

class Package extends Base
{

    /**
     * 套餐列表
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function package_list(){

        $page = input('page', '');

        $data = Db::name('package')->where('packageDel',0)->paginate(10,false,[
            'type'     => 'bootstrap',
            'var_page' => 'page',
            'page' => $page
        ])->toArray();
        foreach($data['data'] as $k=>$v){
            $data['data'][$k]['mainPackage'] = json_decode($v['mainPackage'],true);
            $data['data'][$k]['matchingPackage'] = json_decode($v['matchingPackage'],true);
            $mainPackage = $data['data'][$k]['mainPackage'];
            $matchingPackage = $data['data'][$k]['matchingPackage'];
            if($mainPackage){
                $list = $this->mainPackage_data($mainPackage['main_key'],$mainPackage['main_value']);
                $data['data'][$k]['mainPackageList']= $list;
            }
            if($matchingPackage){
                $add= [];
                foreach($matchingPackage as $n=>$m){
                    $list = $this->mainPackage_data($m['gear_key'],$m['gear_value']);
                    $add[] = $list;
                }

                $data['data'][$k]['matchingPackageList']= $add;
            }
        }

        return json(array(
            'code' => 200,
            'msg' => 'success:获取成功',
            'data' => $data,
        ));
    }

    /**
     * 套餐详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function package_details(){

        $packageId = input('packageId', '');
        if (empty($packageId)) {
            return json(array('code' => 403, 'msg' => 'error:套餐id不能为空'));
        }

        $data = Db::name('package')->where('packageId',$packageId)->find();
        if($data){
            $data['mainPackage'] = json_decode($data['mainPackage'],true);
            $data['matchingPackage'] = json_decode($data['matchingPackage'],true);

            if($data['mainPackage']){
                $list = $this->mainPackage_data($data['mainPackage']['main_key'],$data['mainPackage']['main_value']);
                $data['mainPackageList']= $list;
            }
            if($data['matchingPackage']){
                $add= [];
                foreach($data['matchingPackage'] as $n=>$m){
                    $list = $this->mainPackage_data($m['gear_key'],$m['gear_value']);
                    $add[] = $list;
                }

                $data['matchingPackageList']= $add;
            }
        }
        return json(array(
            'code' => 200,
            'msg' => 'success:获取成功',
            'data' => $data,
        ));
    }

    public function mainPackage_data($main_key,$main_value){

        $list=[];
        //会员
        if($main_key == 1){
            $name =$main_value == 1?'月度会员':'年度会员';
            $list = [
                'id' => $main_value,
                'name' => $name
            ];
        }
        //会员抵扣券
        if($main_key == 2){
            $list = Db::name('voucher')->where('voucherId',$main_value)->field('voucherId as id,voucherName as name')->find();
        }
        //充值卡
        if($main_key == 3){
            switch ($main_value)
            {
                case 1:
                   $name = '1000充值卡';
                break;
                case 2:
                    $name = '2000充值卡';
                break;
                case 3:
                    $name = '5000充值卡';
                    break;
                default:
                    $name = '10000充值卡';
            }
            $list = [
                'id' => $main_value,
                'name' => $name
            ];
        }
        //课程
        if($main_key == 4){
            $list = Db::name('coach_course')->where('courseId',$main_value)->field('courseId as id,courseTitle as name')->find();
        }
        //物品
        if($main_key == 5){
            $list = Db::name('goods')->where('goods_id',$main_value)->field('goods_id as id,goods_name as name')->find();
        }

        return $list;
    }

    /**
     * 提交订单
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addOrderPay(){

        $token = input('token', '');
        $userInfo = $this->getUserInfo($token);
        if (empty($userInfo)) {
            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));
        }

        $packageId = input('packageId', '');
        $orderNote = input('orderNote', '');
        if (empty($packageId)) {
            return json(array('code' => 500, 'msg' => 'error:缺少参数packageId'));
        }

        $package = Db::name('package')->where('packageId', '=', $packageId)
            ->where('packageDel', '=', '0')->find();
        if (empty($package)) {
            return json(array('code' => 500, 'msg' => 'error:获取套餐出错,套餐已下架'));
        }

        $orderPrice = $package['discountedPrice'] == 0?$package['price']:$package['discountedPrice'];

        $catch = array(
            'orderPackageNo' => 'TC' . date('Ymd', time()) . $userInfo['userId'] . rand(1000, 9999),
            'userId' => $userInfo['userId'],
            'orderNum' => 1,
            'orderPrice' => round($orderPrice, 2),
            'orderAddTime' => time(),
            'packageId' => $package['packageId'],
            'packageName' => $package['packageName'],
            'orderNote' => !empty($orderNote) ? $orderNote : false,
        );

        $orderId = Db::name('order_package')->insertGetId($catch);

        return json(array(
            'code' => 200,
            'msg' => 'success:成功',
            'data' => array(
                'orderPackageId' => $orderId,
            ),
        ));
    }
}